//
//  DiscussView.m
//  OperationPro
//
//  Created by Mac on 16/10/9.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "DiscussView.h"
#import "DiscussCell.h"
#import "DiscussTopCell.h"
#import "DiscussModel.h"
#import "NoDataView.h"
@interface DiscussView()<UISearchBarDelegate>
@property (nonatomic, strong) NSMutableArray *totalData;
@property (nonatomic, strong) NSMutableArray *discussData;
@property (nonatomic, strong)UIView *headView;
@property (nonatomic, strong) NSString *keyword;
@property (nonatomic, strong) NSString *kind;
@end

@implementation DiscussView

- (void)initData{
    [super initData];
    _totalData = [NSMutableArray arrayWithObjects:@{}, @{}, nil];
    _discussData = [NSMutableArray array];
}

- (void)initView{
    [super initView];
    [self registerCell:nil withModel:[DiscussModel class]];
    [self setBackgroundColor:[UIColor clearColor]];
    [self setSeparatorStyle:UITableViewCellSeparatorStyleNone];
     self.tableActionType = CTableActionType_Both;
    [self setDataWithUrl:@"discuss_list" andParam:@{}];

    [self refresh];

}



- (void)refresh{
    [CClass HttpRequestTask:NO withUrl:@"discuss_listontop" withData:@{} notifyError:NO onSuccess:^(BaseModel *model) {
        _discussData = [DiscussModel arrayOfModelsFromDictionaries:(NSArray *)model.B_data[@"list"] error:nil];
        self.dataNum=self.discussData.count;
        [self reloadData];
        
    } onError:^(BaseModel *model) {
        
    }];
    [super refresh];
}

- (void)setWithKeyword:(NSString *)keyword kind:(NSString*)kind{
    _keyword = keyword;
    _kind=kind;
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    if ([keyword isEqualToString:@""]) {
        
    }else
    {
        [dic setObject:keyword forKey:@"search"];
        
    }
    if ([kind isEqualToString:@""]) {
        
    }else
    {
        [dic setObject:kind forKey:@"kind"];
        
    }
    [self setDataWithUrl:@"discuss_list" andParam:dic];
    [self refresh];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.scrollView) {
        self.scrollView(scrollView.contentOffset.y);
        return;
    }
    
}
- (void)reloadData{
    if (_discussData) {
        [_totalData replaceObjectAtIndex:0 withObject:_discussData];
    }
    if (_keyword.length > 0|| (_kind && ![_kind isEqualToString:@""])) {
        [_totalData replaceObjectAtIndex:0 withObject:@{}];
    }
   
    if (self.mainData) {
        [_totalData replaceObjectAtIndex:1 withObject:self.mainData];
    }
    NSInteger num=self.mainData.count+self.discussData.count;
    if (self.getData) {
        self.getData(num);
    }
 

    if(self.mainData.count==0&&_discussData.count==0)
    {
        self.tableHeaderView=[self headView];

    }else
    {
        self.tableHeaderView=nil;
    }
    
    [super reloadData];

}
- (UIView*)headView
{
    UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 200)];
    bgView.backgroundColor=BackColor;
    UIImage *logo = [UIImage imageNamed:@"Ico_home_noData"];
    UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake((WindowWidth-logo.size.width)/2, 86, logo.size.width, logo.size.height)];
    logoView.image=logo;
    [bgView addSubview:logoView];
    
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(logoView.frame)+23, WindowWidth, 18)];
    lbl.text = @"没有相关内容";
    [bgView addSubview:lbl];
    lbl.textAlignment=NSTextAlignmentCenter;
    lbl.textColor = C(@"989898");
    [lbl setFont:F(16)];
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _totalData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_totalData[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Class class = [DiscussCell class];
    if (indexPath.section == 0) {
        class = [DiscussTopCell class];
    }
    return [class getCellHeightByModel:[self getRowData:indexPath]];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 10)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Class class = [DiscussCell class];
    if (indexPath.section == 0) {
        class = [DiscussTopCell class];
    }
    NSString *cellID = [NSString stringWithFormat:@"Cell_%@", NSStringFromClass(class)];
    CBaseTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[class alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    [cell setModel:[self getRowData:indexPath]];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.CCellSelect) {
        self.CCellSelect([self getRowData:indexPath]);
    }
}

- (id) getRowData:(NSIndexPath *)indexPath{
    return _totalData[indexPath.section][indexPath.row];
}

//- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//    if (self.DiscussBecomeFirstResponse) {
//        self.DiscussBecomeFirstResponse();
//    }
//    [super touchesEnded:touches withEvent:event];
//}
@end
